/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uy.edu.ort.cliente;

import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import uy.edu.ort.dominio.Cliente;
import uy.edu.ort.dominio.Programa;
import uy.edu.ort.negocio.Consultas;

/**
 *
 * @author 153287-154559
 */
public class FrameConsultas extends javax.swing.JFrame {

    /**
     * Creates new form FrameConsultas
     */
    public FrameConsultas() {
        initComponents();
        this.setLocationRelativeTo(getRootPane());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_TablaResultados = new javax.swing.JTable();
        btn_ConsultaClientes = new javax.swing.JButton();
        btn_ConsultaProgramas = new javax.swing.JButton();
        txt_idCliente = new javax.swing.JTextField();
        txt_nombrePrograma = new javax.swing.JTextField();
        btn_ConsultaCantidad = new javax.swing.JButton();
        btn_ConsultaTamaño = new javax.swing.JButton();
        btn_ProgramasCliente = new javax.swing.JButton();
        lbl_consulta1 = new javax.swing.JLabel();
        lbl_consulta2 = new javax.swing.JLabel();
        lbl_consulta3 = new javax.swing.JLabel();
        lbl_consulta4 = new javax.swing.JLabel();
        lbl_consultas5 = new javax.swing.JLabel();
        lbl_idCliente = new javax.swing.JLabel();
        lbl_nombrePrograma = new javax.swing.JLabel();
        txt_idClientePrograma = new javax.swing.JTextField();
        lbl_idClientePrograma = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consultas");
        setResizable(false);

        tbl_TablaResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbl_TablaResultados);

        btn_ConsultaClientes.setText("Consultar");
        btn_ConsultaClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ConsultaClientesActionPerformed(evt);
            }
        });

        btn_ConsultaProgramas.setText("Consultar");
        btn_ConsultaProgramas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ConsultaProgramasActionPerformed(evt);
            }
        });

        txt_idCliente.setToolTipText("Si no ingresa Id, muestra todos los clientes.");

        txt_nombrePrograma.setToolTipText("Si no se ingresa Nombre, muestra todos los programas.");

        btn_ConsultaCantidad.setText("Consultar");
        btn_ConsultaCantidad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ConsultaCantidadActionPerformed(evt);
            }
        });

        btn_ConsultaTamaño.setText("Consultar");
        btn_ConsultaTamaño.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ConsultaTamañoActionPerformed(evt);
            }
        });

        btn_ProgramasCliente.setText("Consultar");
        btn_ProgramasCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_ProgramasClienteActionPerformed(evt);
            }
        });

        lbl_consulta1.setText("Consulta Clientes");

        lbl_consulta2.setText("Consulta Programas");
        lbl_consulta2.setToolTipText("");

        lbl_consulta3.setText("Programas por tamaño de paginas");

        lbl_consulta4.setText("Programas por cantidad de paginas");

        lbl_consultas5.setText("Programas por cliente");

        lbl_idCliente.setText("Id");

        lbl_nombrePrograma.setText("Nombre");

        txt_idClientePrograma.setToolTipText("Si no ingresa Id, muestra todos los clientes.");

        lbl_idClientePrograma.setText("Id");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbl_consulta1)
                    .addComponent(lbl_consulta2)
                    .addComponent(lbl_consulta4)
                    .addComponent(lbl_consultas5)
                    .addComponent(lbl_consulta3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btn_ProgramasCliente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txt_idClientePrograma, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lbl_idClientePrograma))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btn_ConsultaClientes)
                            .addComponent(btn_ConsultaProgramas)
                            .addComponent(btn_ConsultaCantidad)
                            .addComponent(btn_ConsultaTamaño))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txt_idCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(lbl_idCliente))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txt_nombrePrograma, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lbl_nombrePrograma)))))
                .addGap(34, 34, 34)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 553, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(14, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_ConsultaClientes)
                    .addComponent(txt_idCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbl_idCliente)
                    .addComponent(lbl_consulta1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_ConsultaProgramas)
                    .addComponent(txt_nombrePrograma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbl_nombrePrograma)
                    .addComponent(lbl_consulta2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txt_idClientePrograma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lbl_idClientePrograma))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btn_ProgramasCliente)
                        .addComponent(lbl_consultas5)))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_ConsultaCantidad)
                    .addComponent(lbl_consulta4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_ConsultaTamaño)
                    .addComponent(lbl_consulta3))
                .addGap(69, 69, 69))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_ConsultaClientesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ConsultaClientesActionPerformed
        String idCliente = txt_idCliente.getText().trim();
        List<Cliente> listaClientes;
        if (idCliente.equals("")) {
            listaClientes = Consultas.consultaClientes();
        } else {
            listaClientes = Consultas.consultaClientesPorIdentificador(idCliente);
        }
        DefaultTableModel model = imprimirClientes(listaClientes);
        tbl_TablaResultados.setModel(model);
        if (listaClientes.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "No hay clientes ingresados en el sistema con el identificador requerido.",
                    "Atención", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btn_ConsultaClientesActionPerformed

    private void btn_ConsultaProgramasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ConsultaProgramasActionPerformed
        String nombrePrograma = txt_nombrePrograma.getText().trim();
        List<Programa> listaPrograma;
        if (nombrePrograma.equals("")) {
            listaPrograma = Consultas.consultaProgramas();
        } else {
            listaPrograma = Consultas.consultaProgramasPorNombre(nombrePrograma);
        }
        DefaultTableModel model = imprimirProgramas(listaPrograma);
        tbl_TablaResultados.setModel(model);
        if (listaPrograma.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "No hay programas ingresados en el sistema con el nombre requerido.",
                    "Atención", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btn_ConsultaProgramasActionPerformed

    private void btn_ConsultaCantidadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ConsultaCantidadActionPerformed
        List<Programa> listaPrograma;
        listaPrograma = Consultas.consultaProgramasPorCantidadDePaginas();
        DefaultTableModel model = imprimirProgramas(listaPrograma);
        tbl_TablaResultados.setModel(model);
        if (listaPrograma.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "No hay programas ingresados en el sistema.",
                    "Atención", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btn_ConsultaCantidadActionPerformed

    private void btn_ConsultaTamañoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ConsultaTamañoActionPerformed
        List<Programa> listaPrograma;
        listaPrograma = Consultas.consultaProgramasPorTamañoDePaginas();
        DefaultTableModel model = imprimirProgramas(listaPrograma);
        tbl_TablaResultados.setModel(model);
        if (listaPrograma.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "No hay programas ingresados en el sistema.",
                    "Atención", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btn_ConsultaTamañoActionPerformed

    private void btn_ProgramasClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ProgramasClienteActionPerformed
        String idCliente = txt_idClientePrograma.getText().trim();
        List<Programa> listaProgramas = Consultas.consultaProgramasPorCliente(idCliente);
        DefaultTableModel model = imprimirProgramas(listaProgramas);
        tbl_TablaResultados.setModel(model);
        if (listaProgramas.isEmpty()) {
            JOptionPane.showMessageDialog(rootPane, "No hay programas ingresados en el sistema para el cliente requerido.",
                    "Atención", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btn_ProgramasClienteActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrameConsultas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameConsultas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameConsultas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameConsultas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameConsultas().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_ConsultaCantidad;
    private javax.swing.JButton btn_ConsultaClientes;
    private javax.swing.JButton btn_ConsultaProgramas;
    private javax.swing.JButton btn_ConsultaTamaño;
    private javax.swing.JButton btn_ProgramasCliente;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbl_consulta1;
    private javax.swing.JLabel lbl_consulta2;
    private javax.swing.JLabel lbl_consulta3;
    private javax.swing.JLabel lbl_consulta4;
    private javax.swing.JLabel lbl_consultas5;
    private javax.swing.JLabel lbl_idCliente;
    private javax.swing.JLabel lbl_idClientePrograma;
    private javax.swing.JLabel lbl_nombrePrograma;
    private javax.swing.JTable tbl_TablaResultados;
    private javax.swing.JTextField txt_idCliente;
    private javax.swing.JTextField txt_idClientePrograma;
    private javax.swing.JTextField txt_nombrePrograma;
    // End of variables declaration//GEN-END:variables

    private DefaultTableModel imprimirClientes(List<Cliente> listaClientes) {
        String datos[][] = {};
        String[] colNames = {"ID_CLIENTE", "NOMBRE", "DIRECCION", "TELEFONO", "PAGINA_WEB"};
        DefaultTableModel model = new DefaultTableModel(datos, colNames);
        int row = 0;
        for (Cliente c : listaClientes) {
            model.insertRow(row, new Object[]{});
            model.setValueAt(c.getIdentificador(), row, 0);
            model.setValueAt(c.getNombre(), row, 1);
            model.setValueAt(c.getDireccion(), row, 2);
            model.setValueAt(c.getTelefono(), row, 3);
            model.setValueAt(c.getPagina(), row, 4);
            row++;
        }
        return model;
    }

    private DefaultTableModel imprimirProgramas(List<Programa> listaProgramas) {
        String datos[][] = {};
        String[] colNames = {"NOMBRE", "TAMAÑO", "CANTIDAD_PAGINAS", "ID_CLIENTE"};
        DefaultTableModel model = new DefaultTableModel(datos, colNames);
        int row = 0;
        for (Programa p : listaProgramas) {
            model.insertRow(row, new Object[]{});
            model.setValueAt(p.getNombre(), row, 0);
            model.setValueAt(p.getTamaño(), row, 1);
            model.setValueAt(p.getCantidadPaginas(), row, 2);
            model.setValueAt(p.getCliente().getIdentificador(), row, 3);
            row++;
        }
        return model;
    }
}
